package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.fresh_market.R;
import com.example.fresh_market.entity.ProdCate;

import java.util.List;

import lombok.Getter;

public class ProdCateAdapter extends ListAdapter<ProdCate, ProdCateAdapter.ViewHolder> {

    private final OnItemClickListener mOnItemClickListener;
    @Getter
    private ProdCate currentCate;

    public ProdCateAdapter(OnItemClickListener listener) {
        super(new DiffCallback());
        this.mOnItemClickListener = listener;
    }

    protected ProdCateAdapter(@NonNull DiffUtil.ItemCallback<ProdCate> diffCallback, OnItemClickListener listener) {
        super(diffCallback);
        this.mOnItemClickListener = listener;
    }

    public ProdCate getItem(int position) {
        return super.getItem(position);
    }

    @Override
    @NonNull
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cate_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ProdCate target = getItem(holder.getAdapterPosition());
        holder.bindBean(target);
        //绑定监听事件
        holder.itemView.setOnClickListener(v -> {
            // 获取之前选中的
            int index = currentCate.getIndex();
            getItem(index).setSelect(false);
            notifyItemChanged(index);
            // 获取现在选中的
            int adapterPosition = holder.getAdapterPosition();
            ProdCate cate = getItem(adapterPosition);
            cate.setSelect(true);
            // 把现在选中的设为当前选中的
            currentCate = cate;
            currentCate.setIndex(adapterPosition);
            notifyItemChanged(adapterPosition);
            // 回调接口 进行额外的操作
            mOnItemClickListener.onItemClick(cate, adapterPosition);
        });
    }

    @Override
    public void submitList(@Nullable List<ProdCate> list) {
        if (list != null && !list.isEmpty()) {
            currentCate = list.get(0);
        }
        super.submitList(list);
    }

    public interface OnItemClickListener {
        void onItemClick(ProdCate cate, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;

        ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.TV_drink_type);
        }

        void bindBean(ProdCate target) {
            tvTitle.setText(target.getCateName());
            if (target.isSelect()) {
                itemView.setBackgroundColor(ContextCompat.getColor(itemView.getContext(), R.color.gray));
            } else {
                itemView.setBackgroundColor(ContextCompat.getColor(itemView.getContext(), R.color.white));
            }
        }
    }

    private static class DiffCallback extends DiffUtil.ItemCallback<ProdCate> {
        @Override
        public boolean areItemsTheSame(@NonNull ProdCate oldItem, @NonNull ProdCate newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull ProdCate oldItem, @NonNull ProdCate newItem) {
            return oldItem.equals(newItem);
        }
    }
}